< previous page page_177 next page >

Page 177
because it contains the pointer address. If you left it out, you would be passing a pointer to the EnvironmentBuffer variable itself instead of to the address contained in the variable. The cmdGetStrings_Click function retrieves the environment strings as follows:
Private Sub cmdGetStrings_Click()
   Dim Environment As String
   Dim EnvironmentBuffer As Long
   Dim BufferLength As Long
   Dim CurrentPosition As Long
   Dim NewPosition
    list1.Clear
   EnvironmentBuffer = GetEnvironmentStrings()
   ' Get the length of the buffer
   BufferLength = FindDoubleNull(EnvironmentBuffer)
   ' Allocate space for the data
   Environment = String$(BufferLength, 0)
   ' Copy the string from the buffer
   Call RtlMoveMemory(ByVal Environment, ByVal _
   EnvironmentBuffer, BufferLength)
   Do
      NewPosition = InStr(CurrentPosition + 1, Environment, Chr$(0))
      If NewPosition > CurrentPosition + 1 Then
         List1.AddItem Mid$(Environment, CurrentPosition + 1, _
         NewPosition - CurrentPosition - 1)
         CurrentPosition = NewPosition
      Else
         Exit Do
      End If
   Loop While True
   Call FreeEnvironmentStrings(EnvironmentBuffer)
End Sub
You might be wondering if there isn't a faster way to find the double NULL character. After all, calling a separate RtlMoveMemory function for each byte is a terribly inefficient approach. The answer, fortunately, is yes. The lstrlen API function can determine the length of a string. Since the environment buffer is nothing more than a list of NULL-terminated strings, you can loop through and call it once for each string, as follows:
' Find the number of bytes up to and including the double null
Private Function FindDoubleNull2(ByVal ptr As Long) As Long

 
< previous page page_177 next page >